import java.util.Stack;

class Solution2 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        char[] str = s.toCharArray();
        for (char X : str) {
            if (stack.empty()) {
                return true;
            }
            if (X == '(' || X == '{' || X == '[') {
                stack.push(X);
            } else {
                char ch2 = stack.peek();//左括号
                if (X == ')' && ch2 == '(' || X == '}' && ch2 == '{' || X == ']' && ch2 == '[') {
                    stack.pop();
                } else {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}

public class Text3 {
}
